/*
 * FlightController_Configuration.h
 *
 *  Created on: Feb 3, 2014
 *      Author: ppks
 */

#ifndef FLIGHTCONTROLLER_CONFIGURATION_H_
#define FLIGHTCONTROLLER_CONFIGURATION_H_

#include "config.h"

/********************************************************************************/
/* Constants Definition                                                         */
/********************************************************************************/
#if defined(MULTIPLE_CONFIGURATION_PROFILES)
    #define FC_CONFIG_MAX_SET_NUMBER        3
#else
    #define FC_CONFIG_MAX_SET_NUMBER        1
#endif

#define FC_CONFIG_DEFAULT_SET           0

#define FC_CONFIG_SIZE                  (sizeof(FC_Config))

#define FC_CONFIG_REVISION              0x0001

/********************************************************************************/
/* Types Declaration                                                            */
/********************************************************************************/
/* Global Configuration Info Structure */
typedef struct {
    uint8_t activeSet;
    uint8_t configSetSize;
    uint16_t revision;
} GlobalInfo_t;

/* PID Configuration Structure */
typedef struct {
    uint8_t P8;
    uint8_t I8;
    uint8_t D8;
} PIDConfig_t;

/* Remote Control Configuration Structure */
typedef struct {
    uint8_t rcRate8;
    uint8_t rcExpo8;
    uint8_t rollPitchRate;
    uint8_t yawRate;
    uint8_t dynThrPID;
    uint8_t thrMid8;
    uint8_t thrExpo8;
    int16_t angleTrim[2];
} RCConfig_t;

/* AUX Channel BOX Configuration Structure */
typedef struct {
#if defined(EXTENDED_AUX_STATES)
    uint32_t activate[CHECKBOXITEMS];
#else /* defined(EXTENDED_AUX_STATES) */
    uint16_t activate[CHECKBOXITEMS];
#endif /* defined(EXTENDED_AUX_STATES) */
} BOXConfig_t;


typedef struct {  /* this is a generic way to configure a servo, every multi type with a servo should use it */
  int16_t min;        /* minimum value, must be more than 1020 with the current implementation */
  int16_t max;        /* maximum value, must be less than 2000 with the current implementation */
  int16_t middle;     /* default should be 1500 */
  int8_t  rate;       /* range [-100;+100] ; can be used to ajust a rate 0-100% and a direction */
} servo_conf_;

/* Misc Configuration Structure */
typedef struct {
    uint8_t powerTrigger1;
#if MAG_INSTALLED
    int16_t mag_declination;
#endif
    servo_conf_ servoConf[8];
#if defined(GYRO_SMOOTHING)
    uint8_t Smoothing[3];
#endif /* defined(GYRO_SMOOTHING) */
#if defined(FAILSAFE)
    int16_t failsafe_throttle;
#endif /* defined(FAILSAFE) */
#ifdef VBAT_ENABLED
    uint8_t vbatscale;
    uint8_t vbatlevel_warn1;
    uint8_t vbatlevel_warn2;
    uint8_t vbatlevel_crit;
#endif /* VBAT_ENABLED */
#ifdef POWERMETER
    uint8_t pint2ma;
#endif /* POWERMETER */
#ifdef POWERMETER_HARD
    uint16_t psensornull;
#endif /* POWERMETER_HARD */
#ifdef MMGYRO
    uint8_t mmgyro;
#endif /* MMGYRO */
#ifdef ARMEDTIMEWARNING
    uint16_t armedtimewarning;
#endif /* MMGYRO */
    int16_t minthrottle;
#ifdef GOVERNOR_P
    int16_t governorP;
    int16_t governorD;
#endif /* GOVERNOR_P */
} MISCConfig_t;

/* Global Config Structure Definition*/
typedef struct {
    PIDConfig_t  PID[PIDITEMS];
    RCConfig_t   RC;
    BOXConfig_t  BOX;
    MISCConfig_t MISC;
    uint16_t     checksum;
} GlobalConfig_t;

/********************************************************************************/
/* Exported Variables Declaration                                               */
/********************************************************************************/
extern GlobalInfo_t   ConfigInfo;
extern GlobalConfig_t FC_Config;

extern int16_t lookupPitchRollRC[];
extern int16_t lookupThrottleRC[];

#if 0
/* Pointers to Individual Config Structures */
RCConfig_t*   RCTuning = &FC_Config.RC;
BOXConfig_t*  BOXConfig = &FC_Config.BOX;
MISCConfig_t* MISCConfig = &FC_Config.MISC;
#endif

/********************************************************************************/
/* Exported Functions Prototypes                                                */
/********************************************************************************/
extern void FC_LoadConfigDefaults(void);
extern bool FC_VerifyConfigIntegrity(void);
extern void FC_CalculateRCLookupVectors(void);
extern void FlightController_LoadConfiguration(void);
#endif /* FLIGHTCONTROLLER_CONFIGURATION_H_ */
